package us.icitap.cm.ui.policecase;

import us.icitap.cm.CaseManagementUI;
import us.icitap.cm.business.AppListTypeEnum;
import us.icitap.cm.business.entities.AppEntity;
import us.icitap.cm.business.entities.AppList;
import us.icitap.cm.business.entities.CaseOfficer;
import us.icitap.cm.business.entities.EntityTypeEnum;
import us.icitap.cm.listeners.AppEntityListener;
import us.icitap.cm.ui.custom.CustBeanFieldGroup;
import us.icitap.cm.ui.custom.CustComboBox;
import us.icitap.cm.ui.custom.CustTextField;
import us.icitap.cm.ui.custom.CustomFormLayout;
import us.icitap.cm.ui.shared.AppEntityLayout;
import us.icitap.cm.ui.shared.OfficerField;
import us.icitap.cm.utils.ProjectLists;

import com.vaadin.data.fieldgroup.FieldGroup.CommitException;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.TabSheet;
import com.vaadin.ui.VerticalLayout;

public class OfficerDataLayout extends AppEntityLayout{

	/**
	 * 
	 */
	private static final long serialVersionUID = 4156093840005232478L;

	private CaseOfficer caseOfficer;
	private final CustBeanFieldGroup<CaseOfficer> binder = new CustBeanFieldGroup<CaseOfficer>(CaseOfficer.class);
	
	
	private OfficerField officerField = new OfficerField();
	
	private CustTextField phoneNoTextField = new CustTextField("Nr. telefoni");
	
	private CustComboBox caseInvolvementSelect = new CustComboBox("Perfshirja me ceshtjen");
	
	
	final CustomFormLayout cfl = new CustomFormLayout("550px", "Organizate e perfshire ne ngjarje", "Reset", "Ruaj");
	
	protected AppEntityListener listener;
	
	boolean editableVisible = false;
	boolean readOnlyVisible = false;
	
	

	final TabSheet tabSheet = new TabSheet();
	
	public OfficerDataLayout() {
		
		VerticalLayout vl = new VerticalLayout();
		vl.addStyleName("cmLayout");
		vl.setMargin(true);
		vl.addComponent(officerField);
		
		FormLayout fl = new FormLayout();
		fl.addComponent(phoneNoTextField);
		fl.addComponent(caseInvolvementSelect);
		
		vl.addComponent(fl);
		
		phoneNoTextField.setWidth("300px");
		
		caseInvolvementSelect.setContainerDataSource(new BeanItemContainer<AppList>(AppList.class, ProjectLists.getList(AppListTypeEnum.CaseOfficerInvolvement, false)));
		caseInvolvementSelect.setWidth("300px");
		
		
		tabSheet.addTab(vl, "Te dhenat");
		
		
		
		cfl.addComponent(tabSheet);
		cfl.addFooter();
		
		cfl.addRightClickListener(new ClickListener() {
			
			@Override
			public void buttonClick(ClickEvent event) {
				try {
					binder.commit();
					if(OfficerDataLayout.this.listener != null){
						if(caseOfficer.getAppEntity().isNewRecord()){
							OfficerDataLayout.this.listener.addAppEntityToReport(caseOfficer.getAppEntity());
						}
						else
							if(!oldAppEntity.getCaseOfficer().myEquals(caseOfficer)){
								caseOfficer.getAppEntity().setRecordUser(CaseManagementUI.getUser().getUsername());
								OfficerDataLayout.this.listener.updateAppEntityIntoReport(oldAppEntity, caseOfficer.getAppEntity());
							}
							else
							{
//								oldAppEntity.replaceEntityReportAssociationList(caseOfficer.getAppEntity().getReportAssociationList());
								OfficerDataLayout.this.listener.refresh(oldAppEntity);
							}
						
						addAppEntity();
					}
				} catch (CommitException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});

		binder.bind(this.officerField, "officer");
		binder.bind(this.phoneNoTextField, "phoneNo");
		binder.bind(this.caseInvolvementSelect, "caseInvolvement");
		
	}
	
	public void showEditableForm(){
		if(editableVisible) return ;
		
		this.removeAllComponents();
		this.addComponent(cfl);
		
		editableVisible = true;
		readOnlyVisible = false;
	}
	

	public void showReadOnlyForm(){
		if(readOnlyVisible) return;
		this.removeAllComponents();
		VerticalLayout vl = ViewCaseLayout.showOfficer(caseOfficer);
		vl.setCaption(EntityTypeEnum.OFFICER.getCaption());
		this.addComponent(vl);
		editableVisible = false;
		readOnlyVisible = true;
	}
	
	@Override
	public void addAppEntity() {
		this.tabSheet.setSelectedTab(0);
		AppEntity appEntity = new AppEntity(CaseManagementUI.getUser().getUsername(), EntityTypeEnum.OFFICER);
		caseOfficer = appEntity.getCaseOfficer();
		binder.setItemDataSource(caseOfficer);
		
		this.showEditableForm();

	}

	@Override
	public void addAppEntityListener(AppEntityListener listener) {
		this.listener = listener;
		
	}

	private AppEntity oldAppEntity;
	
	@Override
	public void loadAppEntity(AppEntity appEntity, boolean editable) {

		oldAppEntity = appEntity;
		
		AppEntity newAppEntity = new AppEntity(appEntity);
		
		this.caseOfficer = newAppEntity.getCaseOfficer();
		binder.setItemDataSource(caseOfficer);

		if(editable) 
			this.showEditableForm();
		
		else
			this.showReadOnlyForm();
	
	}

}
